package org.jeecg.config.mybatis;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.jeecg.common.util.oConvertUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * 单数据源配置（jeecg.datasource.open = false时生效）
 * @Author zhoujf
 *
 */
@Configuration
@MapperScan(value={"org.jeecg.modules.**.mapper*"})
public class MybatisPlusSaasConfig {
    /**
     * tenant_id 字段名
     */
    private static final String TENANT_FIELD_NAME = "tenant_id";
    /**
     * 哪些表需要做多租户 表需要添加一个字段 tenant_id
     */
    private static final List<String> tenantTable = new ArrayList<String>();

    static {
        tenantTable.add("demo");

        tenantTable.add("sys_depart");
        tenantTable.add("sys_variable");
        tenantTable.add("sys_quartz_job");
        tenantTable.add("jw_web_jwid");
        tenantTable.add("jw_web_msg_template");
        tenantTable.add("jw_mp_msg_record");

//        //角色、菜单、部门
//        tenantTable.add("sys_role");
//        tenantTable.add("sys_permission");
//        tenantTable.add("sys_depart");

        //arc 基础数据
        tenantTable.add("mws_t_base_areas");
        //收费项目
        tenantTable.add("mws_t_base_charge_item");

        tenantTable.add("mws_t_base_natures");
        tenantTable.add("mws_t_base_water_prices");
        //tenantTable.add("mws_t_base_sub_water_prices");

        tenantTable.add("mws_t_base_period");
        tenantTable.add("mws_t_arc_adjusted_configs");
        tenantTable.add("mws_t_arc_adjusted_meters");

        tenantTable.add("mws_t_arc_contacts");
        tenantTable.add("mws_t_arc_customers");
        tenantTable.add("mws_t_arc_account");
        tenantTable.add("mws_t_arc_balance");
        tenantTable.add("mws_t_arc_account_meter");
        tenantTable.add("mws_t_arc_cus_record");//修改记录
        tenantTable.add("mws_t_arc_meter_replaces");

        //bl/ms 策略计量
        tenantTable.add("mws_t_bl_discounts");
        tenantTable.add("mws_t_bl_discount_meters");
        tenantTable.add("mws_t_bl_discount_fees");
        tenantTable.add("mws_t_bl_sub_discounts");
        tenantTable.add("mws_t_bl_expenses");
        tenantTable.add("mws_t_bl_sub_expenses");
        tenantTable.add("mws_t_ms_measures");
        //tenantTable.add("mws_t_ms_sub_measures");

        //bv 票据
        tenantTable.add("mws_t_bv_versions");
        tenantTable.add("mws_t_bv_storages");
        tenantTable.add("mws_t_bv_vouchers");
        tenantTable.add("mws_t_bv_scrappeds");
        tenantTable.add("mws_t_bv_sub_vouchers");

        //chg 费用
        tenantTable.add("mws_t_chg_arrears");
        tenantTable.add("mws_t_chg_sub_arrear");
        tenantTable.add("mws_t_chg_arrear_expenses");
        tenantTable.add("mws_t_chg_strikes");
        tenantTable.add("mws_t_chg_storeds");
        tenantTable.add("mws_t_chg_derate");
        tenantTable.add("mws_t_chg_payments");
        tenantTable.add("mws_t_chg_sub_payments");
        tenantTable.add("mws_t_chg_fees");
        tenantTable.add("mws_t_chg_sub_fees");
        tenantTable.add("mws_t_his_arrear_expenses");
        tenantTable.add("mws_t_his_arrears");
        tenantTable.add("mws_t_his_sub_arrear");

        //mr 抄表
        tenantTable.add("mws_t_mr_ajusted");
        tenantTable.add("mws_t_mr_audits");
        tenantTable.add("mws_t_mr_books");
        tenantTable.add("mws_t_mr_book_meters");
        tenantTable.add("mws_t_mr_changes");
        tenantTable.add("mws_t_mr_records");
        tenantTable.add("mws_t_mr_unread");
    }

    public boolean ignoreTable(String tableName) {
        for(String temp: tenantTable){
            if(temp.equalsIgnoreCase(tableName)){
                return false;
            }
        }
        return true;
    }


    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor
        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
            @Override
            public Expression getTenantId() {
                String tenant_id = oConvertUtils.getString(TenantContext.getTenant(),"0");
                return new LongValue(tenant_id);
            }

            @Override
            public String getTenantIdColumn(){
                return TENANT_FIELD_NAME;
            }

            // 返回 true 表示不走租户逻辑
            @Override
            public boolean ignoreTable(String tableName) {
                for(String temp: tenantTable){
                    if(temp.equalsIgnoreCase(tableName)){
                        return false;
                    }
                }
                return true;
            }
        }));
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }

//    /**
//     * 下个版本会删除，现在为了避免缓存出现问题不得不配置
//     * @return
//     */
//    @Bean
//    public ConfigurationCustomizer configurationCustomizer() {
//        return configuration -> configuration.setUseDeprecatedExecutor(false);
//    }
//    /**
//     * mybatis-plus SQL执行效率插件【生产环境可以关闭】
//     */
//    @Bean
//    public PerformanceInterceptor performanceInterceptor() {
//        return new PerformanceInterceptor();
//    }

}
